package com.xingdata.jjxc.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/** 
 * @author ���� E-mail: 13552269257@139.com
 * @version ����ʱ�䣺2013-1-10 ����9:46:33 
 * ��˵�� 
 */
public class DateTime 
{
	/**
	 * @param date �������ַ�
	 * @return ����
	 * @throws ParseException 
	 */
	public static Date Convert(String date) 
	{
		String strFormat = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat dd=new SimpleDateFormat(strFormat);
		try {
			return dd.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @param date ����
	 * @return �������ַ�
	 */
	public static String ToString(Date date)
	{
		String strFormat = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat dd=new SimpleDateFormat(strFormat);
		return dd.format(date).toString();
	}
	/**
	 * 年月日
	 * @param date
	 * @return
	 */
	public static String toChenseDate(Date date)
	{
		String strFormat = "yyyy年MM月dd日";
		SimpleDateFormat dd=new SimpleDateFormat(strFormat);
		return dd.format(date).toString();
	}
	/**
	 * @param date ����
	 * @return �������ַ�
	 */
	public static String ToString1(Date date)
	{
		String strFormat = "yyyyMMddHHmmss";
		SimpleDateFormat dd=new SimpleDateFormat(strFormat);
		return dd.format(date).toString();
	}
	/**
	 * 将String 转成long
	 * @return
	 */
	public static long getTimestamp(String time) {
        long rand = 0;
        try {
    
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date d2 = null;
            try {
                d2 = sdf.parse(time);//将String to Date类型
                rand = d2.getTime();
            } catch (ParseException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rand;
    } 
	
	public static String GetShiChang(long times)
	{
		 //Log.i("毫秒数",String.valueOf(times));
		 long TotalSecond=times/1000;
		 //Log.i("总秒数",String.valueOf(TotalSecond));
		 long CallSecond=TotalSecond%60;
		 //Log.i("秒",String.valueOf(CallSecond));
		 long CallMinutes=(TotalSecond/60)%60;
		 //Log.i("分",String.valueOf(CallMinutes));
		 long CallHour=(TotalSecond/60)/60;
		// Log.i("时",String.valueOf(CallHour));
		 String timesString=String.valueOf(CallHour)+"小时"+String.valueOf(CallMinutes)+"分钟"+String.valueOf(CallSecond)+"秒";
	    return timesString;

	}
	
	/**
	 * @param date �������ַ�
	 * @return ����
	 * @throws ParseException 
	 */
	public static Calendar ConvertToCalendar(String date) 
	{
		return ConvertToCalendar(date,"yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * @param date �������ַ�
	 * @return ����
	 * @throws ParseException 
	 */
	public static Calendar ConvertToCalendar(String date,String format) 
	{
		SimpleDateFormat dd=new SimpleDateFormat(format);
		try {
				Calendar calendar = Calendar.getInstance();
			
				if(date.length() > 0)
				{
					calendar.setTime(dd.parse(date));
				}
				
				return calendar;
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return null;
	}
	
	/**
	 * @param Calendar ����
	 * @return �������ַ�
	 */
	public static String ConvertToString(Calendar date)
	{
		return ConvertToString(date,"yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * @param Calendar ����
	 * @return �������ַ�
	 */
	public static String ConvertToString(Calendar date,String format)
	{
		SimpleDateFormat dd=new SimpleDateFormat(format);
		
		return dd.format(date.getTime());
	}
	
	public static String ConvertToMyString(Calendar date)
	{
		String format="yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat dd=new SimpleDateFormat(format);
		String time = dd.format(date.getTime());
		time = time.replace("-", "");
		time=time.replace(" ", "");
		return time;
	}
	public static String getDate() {
		Calendar c = Calendar.getInstance();
		String year = String.valueOf(c.get(Calendar.YEAR));
		String month = String.valueOf(c.get(Calendar.MONTH));
		String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
		String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
		String mins = String.valueOf(c.get(Calendar.MINUTE));
		StringBuffer sbBuffer = new StringBuffer();
		sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":"
				+ mins);
		return sbBuffer.toString();
	}
	public static String getChenseDate(){
		return toChenseDate(new Date());
	}
	public static String toChenseTime(Date date)
	{
		String strFormat = "HH:mm";
		SimpleDateFormat dd=new SimpleDateFormat(strFormat);
		return dd.format(date).toString();
	}
}
